package hmvictor.jpql;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.openide.windows.WindowManager;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Victor
 */
public final class PersistenceUnitHelper {

    public static Connection getDefaultConnection(String unitname) throws MalformedURLException, SAXException, IOException, XPathExpressionException, ParserConfigurationException {
        JarExplorerTopComponent c = (JarExplorerTopComponent) WindowManager.getDefault().findTopComponent("JarExplorerTopComponent");
        ClassLoader classloader = c.createClassLoader();
        Connection connection = null;
        Enumeration<URL> resources = classloader.getResources("META-INF/persistence.xml");
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression exprPersistenceUnit = xpath.compile("persistence/persistence-unit[@name='" + unitname + "']");
        XPathExpression exprUrl = xpath.compile("./properties/property[@name='javax.persistence.jdbc.url']/@value");
        XPathExpression exprPassword = xpath.compile("./properties/property[@name='javax.persistence.jdbc.user']/@value");
        XPathExpression exprUser = xpath.compile("./properties/property[@name='javax.persistence.jdbc.password']/@value");
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(resource.openStream());
            NodeList nl = (NodeList) exprPersistenceUnit.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < nl.getLength(); i++) {
                Node node = nl.item(i);
                NodeList listUrl = (NodeList) exprUrl.evaluate(node, XPathConstants.NODESET);
                NodeList listUser = (NodeList) exprUser.evaluate(node, XPathConstants.NODESET);
                NodeList listPassword = (NodeList) exprPassword.evaluate(node, XPathConstants.NODESET);
                if (listUrl.getLength() > 0) {
                    connection = new Connection();
                    connection.setName("Default");
                    connection.setUrl(listUrl.item(i).getNodeValue());
                }
                if (listUser.getLength() > 0 && connection != null) {
                    connection.setUser(listUser.item(i).getNodeValue());
                }
                if (listPassword.getLength() > 0 && connection != null) {
                    connection.setPassword(listPassword.item(i).getNodeValue().toCharArray());
                }
            }
        }
        return connection;
    }

}
